package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDept;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.service.ITypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 品种类别 信息操作处理
 *
 * @author ruoyi
 * @date 2019-04-22
 */
@Controller
@RequestMapping("/system/types")
public class TypeController extends BaseController
{
	private String prefix = "system/types";

	@Autowired
	private ITypeService typeService;

	@RequiresPermissions("system:types:view")
	@GetMapping()
	public String type()
	{
		return prefix + "/type";
	}
	/**
	 * 查询品种类别列表
	 */
	@RequiresPermissions("system:types:list")
	@GetMapping("/list")
	@ResponseBody
	public List<Type> list(Type type)
	{
//		startPage();
		List<Type> list = typeService.selectTypeList(type);
		return list;
	}

	/**
	 * 选择类别树
	 */
	@GetMapping("/selectTypeTree/{typeId}")
	public String selectTypeTree(@PathVariable("typeId") Long typeId, ModelMap mmap)
	{
		mmap.put("type", typeService.selectTypeById(typeId));
		return prefix + "/tree";
	}

	/**
	 * 加载部门列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Ztree> treeData()
	{
		List<Ztree> ztrees = typeService.selectTypeTree(new Type());
		return ztrees;
	}


	/**
	 * 校验部门名称
	 */
	@PostMapping("/checkTypeNameUnique")
	@ResponseBody
	public String checkTypeNameUnique(Type type)
	{
		return typeService.checkTypeNameUnique(type);
	}

	/**
	 * 导出品种类别列表
	 */
	@RequiresPermissions("system:types:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Type type)
	{
		List<Type> list = typeService.selectTypeList(type);
		ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
		return util.exportExcel(list, "type");
	}

	/**
	 * 新增品种类别
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
	{
		mmap.put("type", typeService.selectTypeById(parentId));
		return prefix + "/add";
	}

	/**
	 * 新增保存品种类别
	 */
	@RequiresPermissions("system:types:add")
	@Log(title = "品种类别", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Type type)
	{
		return toAjax(typeService.insertType(type));
	}

	/**
	 * 修改品种类别
	 */
	@GetMapping("/edit/{typeId}")
	public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
	{
		Type type = typeService.selectTypeById(typeId);
		if (StringUtils.isNotNull(type) && 100L == typeId)
		{
			type.setParentName("无");
		}
		mmap.put("type", type);

		return prefix + "/edit";
	}


	/**
	 * 修改保存品种类别
	 */
	@RequiresPermissions("system:types:edit")
	@Log(title = "品种类别", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Type type)
	{
		return toAjax(typeService.updateType(type));
	}

	/**
	 * 删除品种类别
	 */
	@RequiresPermissions("system:types:remove")
	@Log(title = "品种类别", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(typeService.deleteTypeByIds(Long.valueOf(ids)));
	}


	/**
	 * 删除品种类别
	 */
	@Log(title = "品种类别", businessType = BusinessType.DELETE)
	@RequiresPermissions("system:types:remove")
	@GetMapping("/remove/{typeId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("typeId") Long typeId)
	{
		if (typeService.selectTypeCount(typeId) > 0)
		{
			return AjaxResult.warn("存在下级部门,不允许删除");
		}
		if (typeService.checkTypeExistInventory(typeId))
		{
			return AjaxResult.warn("类别存在商品,不允许删除");
		}
		return toAjax(typeService.deleteTypeByIds(typeId));
	}

	/**
	 * ��ȡƷ�ַ���
	 * @param id
	 * @return
	 */
	@GetMapping("/getLevel2/{id}")
	@ResponseBody
	public AjaxResult getLevel2(@PathVariable("id") Long id) {
		Type level = new Type();
		level.setParentId(id);
		List<Type> list = typeService.selectTypeList(level);
		AjaxResult ajaxResult = new AjaxResult();
		ajaxResult.put("data",list);
		return ajaxResult;
	}

}
